package com.eternal.main.model;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;
import androidx.databinding.BindingConversion;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eternal.base.concat.DeviceInfo;
import com.eternal.base.concat.DeviceTFP;
import com.eternal.base.concat.PortInfo;
import com.eternal.base.global.ActivityEvent;
import com.eternal.base.global.BluetoothKey;
import com.eternal.base.protocol.ProtocolTransformer;
import com.eternal.base.router.RouterActivityPath;
import com.eternal.framework.binding.command.BindingAction;
import com.eternal.framework.binding.command.BindingCommand;
import com.eternal.framework.bus.Messenger;
import com.eternal.framework.component.AppManager;
import com.eternal.framework.utils.Utils;
import com.eternal.main.MainActivity;
import com.eternal.main.R;

/**
 * Created by Administrator
 * On 2020/4/24
 * Description:
 *
 * @author Administrator
 */
public class PortModel {
    private String mac;
    private boolean isConnected;
    private byte portId;
    public ObservableInt fanState = new ObservableInt();
    public ObservableField<String> fanSize = new ObservableField<>();
    public ObservableBoolean fanVisibility = new ObservableBoolean();
    public ObservableBoolean fan2Visibility = new ObservableBoolean();
    public ObservableBoolean dashVisibility = new ObservableBoolean();
    public ObservableBoolean visibility = new ObservableBoolean();
    public ObservableField<String> name = new ObservableField<>();
    public ObservableBoolean isPlug = new ObservableBoolean();

    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public BindingCommand<Void> onClick = new BindingCommand<Void>(new BindingAction() {
        @Override
        public void call() {
            if (listener != null) {
                listener.onClick(portId);
            }
        }
    });

    interface OnClickListener {
        void onClick(byte id);
    }

    PortModel() {
        super();
    }

    PortModel(PortInfo info) {
        if (info.fanType == 0) {
            fanVisibility.set(true);
            fanState.set(getState(info.fanState));
            fanSize.set(String.valueOf(info.fan));
        } else if (info.fanType == 1) {
            fanVisibility.set(false);
            fanSize.set(info.fan == 0 ? "OFF" : "ON");
        } else {
            fanVisibility.set(false);
            fanSize.set("ON");
        }
    }

    void update(PortInfo info) {
        portId = info.id;
        visibility.set(true);
        name.set(TextUtils.isEmpty(info.name) ? "Port "+info.id : info.name);
        if (info.isPlug) {
//            if (info.fanType == 0) {
                fanVisibility.set(true);
                fan2Visibility.set(false);
                fanState.set(getState(info.fanState));
                fanSize.set(String.valueOf(info.fan));

//            } else if (info.fanType == 1) {
//                fanVisibility.set(false);
//                fan2Visibility.set(true);
//                fanSize.set(info.fan == 0 ? "OFF" : "ON");
//            } else {
//                fanVisibility.set(false);
//                fan2Visibility.set(true);
//                fanSize.set("ON");
//            }
            isPlug.set(true);
            dashVisibility.set(false);
        } else {
            isPlug.set(false);
            fanVisibility.set(false);
            fan2Visibility.set(false);
            dashVisibility.set(true);
        }
    }

    private int getState(byte state) {
        switch (state) {
            case 0:
                return R.mipmap.stead_trend;
            case 1:
                return R.mipmap.decrease_trend;
            default:
                return R.mipmap.increase_trend;
        }
    }
}
